/*
 * These files are distributed under the terms of the GNU GPLv2, the GNU General
 * Public version 2. See LICENSE.txt
 */
package fileCommodore.plugin.file_info;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;

import fileCommodore.model.*;
import fileCommodore.plugin.*;

/**
 * @author nado18@gmail.com, on Jul 26, 2007
 */
public class FileInfoPlugin extends VisiblePlugin
{
	private JPanel pane = new JPanel( new GridLayout( 2, 2 ) );
	private GroupInfoPane taggedPane = new GroupInfoPane( "Tagged" );
	private GroupInfoPane untaggedPane = new GroupInfoPane( "Untagged" );
	private GroupInfoPane selectedPane = new GroupInfoPane( "Selected" );
	private GroupInfoPane unselectedPane = new GroupInfoPane( "Unselected" );
	
	private Selection os;
	private Selection s;
	
	public FileInfoPlugin()
	{
		pane.add( taggedPane );
		pane.add( untaggedPane );
		pane.add( selectedPane );
		pane.add( unselectedPane );
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see plugin.Visible#getPluginComponent()
	 */
	public JComponent getPluginComponent()
	{
		return pane;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see plugin.Visible#wantFocusOnTrigger()
	 */
	public boolean wantFocusOnTrigger()
	{
		return false;
	}
	
	private boolean differ( Vector<FileRowItem> A, Vector<FileRowItem> B )
	{
		if (null == A && null == B)
		{
			return false;
		}
		else if (null == A)
		{
			return true;
		}
		else if (null == B)
		{
			return true;
		}
		else
		{
			boolean retval = A.size() != B.size() || !A.containsAll( B );
			return retval;
		}
	}
	
	private void update()
	{
		if (isVisible())
		{
			if (null == s || null == os
					|| differ( s.getTagged(), os.getTagged() ))
			{
				taggedPane.setItems( s.getTagged() );
			}
			
			if (null == s || null == os
					|| differ( s.getUntagged(), os.getUntagged() ))
			{
				untaggedPane.setItems( s.getUntagged() );
			}
			
			Vector<FileRowItem> selected = new Vector<FileRowItem>();
			Vector<FileRowItem> oSelected = new Vector<FileRowItem>();
			selected.add( s.getSelected() );
			oSelected.add( os.getSelected() );
			if (null == s || null == os || differ( selected, oSelected ))
			{
				selectedPane.setItems( selected );
			}
			
			if (null == s || null == os
					|| differ( s.getUnselected(), os.getUnselected() ))
			{
				unselectedPane.setItems( s.getUnselected() );
			}
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see plugin.Plugin#activeSelectionChanged()
	 */
	@Override
	public void activeSelectionChanged()
	{
		Selection selection = getActiveSelection();
		s = selection;
		update();
		os = selection;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see plugin.Plugin#handle(java.awt.event.KeyEvent)
	 */
	@Override
	public void handle( KeyEvent ev )
	{
		if (KeyEvent.VK_F12 == ev.getKeyCode()
				|| ( KeyEvent.VK_I == ev.getKeyCode() && isAltDown( ev ) ))
		{
			displayInInactivePane();
			update();
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see plugin.Plugin#inactiveSelectionChanged()
	 */
	@Override
	public void inactiveSelectionChanged()
	{
		// TODO Auto-generated method stub
	}
}
